const errorFun = (err) => {
    if (typeof err === 'string') {
        return
    }
    wx.showModal({
        title: '温馨提示',
        content: JSON.stringify(err),
        showCancel: true,
        confirmText: '确定',
    })
}

const throttle = function (fn, delay) {
    let preTime = 0;
    delay = !delay || delay == null ? 1000 : delay
    return function () {
        const context = this;
        const args = arguments; s
        const now = +new Date();
        if (now - preTime > delay) {
            fn.apply(context, args);
            preTime = now;
        }
    };
}

const formatDate = (date) => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const secs = date.getSeconds();
    return [year, month, day].map(formatNumber).join("-") + "（" + [hour, minute, secs].map(formatNumber).join(':') + "）"
};

const formatNumber = (n) => {
    n = n.toString();
    return n[1] ? n : "0" + n;
};

const getDayTime = () => {
    let now = new Date();
    let year = now.getFullYear();
    let month = now.getMonth();
    let date = now.getDate();
    let hour = now.getHours();
    let minute = now.getMinutes();
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minute < 10) minute = "0" + minute;
    return `${year}年${month}月${date}日 ${hour}:${minute}`
}

const orderNum = () => {
    let d = new Date().getTime();

    let uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        let r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16);
    });
    return uuid;
}

const getType = function (params) {
    return Object.prototype.toString.call(params).slice(8, -1);
}


const loadCustomFont = function () {
    wx.loadFontFace({
        global: true,
        family: 'custom_font',
        source:
            'url("https://qncdn.mopic.mozigu.net/f/o2ob83i18ijr70tmshq/RuiZiZhenYanTiMianFeiShangYong-2.ttf")',
    });
}

export {
    errorFun,
    throttle,
    formatDate,
    formatNumber,
    getDayTime,
    orderNum,
    getType,
    loadCustomFont
}
